package com.github.cyf.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author chenyifan
 * @create 2024-11-15 14:27
 */
@Slf4j
public class StartUtil {

    public static void run(ConfigurableApplicationContext application) throws UnknownHostException {
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String contextPath = env.getProperty("server.servlet.context-path");
        contextPath = !StringUtils.hasText(contextPath) ? "" : contextPath;
        String profiles = env.getProperty("spring.active.profiles");
        String localPath = "http://localhost:" + port + contextPath;
        String externalPath = "http://" + ip + ":" + port + contextPath;
        String swaggerPath = "http://" + ip + ":" + port + contextPath + "/doc.html";

        StringBuilder builder = new StringBuilder();
        builder.append("\n\n༼ つ ◕_◕ ༽つ 起来惹\t──────────────────────────────────────────<<<\n")
                .append("|\tprofiles: \t").append(profiles).append("\n")
                .append("│\tLocal: \t\t").append(localPath).append("\n")
                .append("│\tExternal: \t").append(externalPath).append("\n")
                .append("│\tswagger: \t").append(swaggerPath).append("\n")
                .append("└─────────────────────────────────────────>>> o(*////▽////*)q Excited…\n");
        log.info(builder.toString());
    }
}